<template>
  <el-dialog :visible="showDialog" :title="showTitle" @close="close">
    <el-form ref="deptsForm" label-width="120px" :rules="rules" :model="formDeParts">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formDeParts.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formDeParts.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formDeParts.manager" style="width:80%" placeholder="请选择" @focus="getManager">
          <el-option v-for="item in managers" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formDeParts.introduce" type="select" rows="4" style="width:80%" placeholder="1-300个字符" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="8">
        <el-button size="small" @click="close">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addDepartments, getDepartments, getEmployeeSimple, getDepartDetail, emitDepartDetail } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    // 自定义校验部门名称
    const checkName = async(rules, value, callback) => {
      let isRepeat = false
      const { depts } = await getDepartments()
      // 获取部门列表数据
      if (this.formDeParts.id) {
        // 编辑
        isRepeat = depts.filter(item => item.pid === this.formDeParts.id && item.id !== this.formDeParts.id).some(item => item.name === value)
      } else {
        // 新增
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }

      isRepeat ? callback(new Error('部门名称已存在')) : callback()
    }

    // 自定义校验部门编码
    const checkCode = async(rules, value, callback) => {
      // 获取部门列表数据
      let isRepeat = false
      const { depts } = await getDepartments()
      if (this.formDeParts.id) {
        // 编辑
        isRepeat = depts.some(item => item.pid === this.treeNode.id && item.id !== this.formDeParts.id && item.code === value)
      } else {
        isRepeat = depts.some(item => item.pid === this.treeNode.id && item.code === value)
      }

      isRepeat ? callback(new Error('部门编码已存在')) : callback()
    }
    return {
      formDeParts: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      managers: [], // 部门负责人列表
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkName }
        ],
        code: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCode }
        ],
        manager: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }, { min: 1, max: 300, message: '部门名称要求1-300个字符', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formDeParts.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async getManager() {
      this.managers = await getEmployeeSimple()
    },
    // 新增
    btnOk() {
      // 触发父组件事件
      // this.$emit('closeDialog', false)
      this.$refs.deptsForm.validate(async(isOk) => {
        if (isOk) {
          if (this.formDeParts.id) {
            await emitDepartDetail(this.formDeParts)
          } else {
            // console.log('校验成功')
            await addDepartments({ ...this.formDeParts, pid: this.treeNode.id })
          }
          // 告知父亲新增了一条数据, 需要重新获取新增后的数据
          this.$emit('addDepts')
          // 语法糖
          this.$emit('update:showDialog', false)
        }
      })
    },
    // 取消
    close() {
      // 重置表单数据( 上一次的操作是 新增/编辑  表单输入框的数据还有  要清除掉)
      this.$refs.deptsForm.resetFields()
      // 关闭弹框
      this.$emit('update:showDialog', false)
    },
    // 获取部门详情
    async getDepartDetail(id) {
      this.formDeParts = await getDepartDetail(id)
    }
  }
}
</script>

<style>

</style>
